package com.sjq.common.web.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sjq.common.entity.JsTreeNode;
import com.sjq.common.entity.MenuTree;
import com.sjq.common.service.MenuTreeService;

/**
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/api/v1/sys-manage/menu-manager")
public class MenuTreeRestController {

	Logger logger = Logger.getLogger(MenuTreeRestController.class);
	
	@Autowired
	private MenuTreeService menuTreeService;
	
	public static final String ROOT_PID = "0";
	
	@RequestMapping("/menuList")
	List<?> findMenuTree(){
		List<?> allMenuTree=menuTreeService.findAllJsTree();
		return allMenuTree;
	}
	
	@RequestMapping("/get-childrenMenu")
	public List<JsTreeNode> findJsTreeChildren() {

		List<Map<String, Object>> jsTreeChildrens = menuTreeService
				.findAllJsTree();
		List<MenuTree> jsTreeChildren = mapToMenuTree(jsTreeChildrens);

		MenuTree jsTree = menuTreeService.findSingleJsTree(ROOT_PID);
		JsTreeNode treeNode = new JsTreeNode();
		treeNode.setId(jsTree.getMenuId());
		List<JsTreeNode> jsTreeNode = makeJsTreeNode(treeNode, jsTree.getParentMenuId(),
				jsTreeChildren, 1);
		return jsTreeNode;
	}

	@RequestMapping("/get-singleMenu")
	public MenuTree getSingleTreeNode(String pid) {
		if (pid == null) {
			pid = ROOT_PID;
		}
		MenuTree jsTree = menuTreeService.findSingleJsTree(pid);

		return jsTree;
	}

	private List<JsTreeNode> makeJsTreeNode(JsTreeNode fatherNode, String pid,
			List<MenuTree> jsTreeChildren, Integer index) {
		List<JsTreeNode> childTreeNode = new ArrayList<JsTreeNode>();
		for (MenuTree childTree : jsTreeChildren) {
			if (!childTree.getParentMenuId().equals(pid)) {
				if (childTree.getParentMenuId() == null
						|| "".equals(childTree.getParentMenuId().trim())) {
					break;
				}
				continue;
			}

			JsTreeNode childNode = new JsTreeNode();
			childNode.setId(childTree.getMenuId());
			childNode.setName(childTree.getMenuName());
			childNode.setPid(childTree.getParentMenuId());
			
			childNode.setOrderno(childTree.getOrderno());
			List<JsTreeNode> list = makeJsTreeNode(childNode,
					childTree.getMenuId(), jsTreeChildren, ++index);

			if (list != null && list.size() != 0) {
				childNode.setChildren(list);
			}
			childTreeNode.add(childNode);
		}
		return childTreeNode;
	}

	private List<MenuTree> mapToMenuTree(List<Map<String, Object>> list) {
		List<MenuTree> jsTrees = new ArrayList<MenuTree>();
		Map<String, Object> map = null;
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				MenuTree jsTree = new MenuTree();
				map = list.get(i);
				if (null != map.get("cd_id")) {
					jsTree.setMenuId(map.get("cd_id").toString());
				}
				if (null != map.get("fcd_id")) {
					jsTree.setParentMenuId(map.get("fcd_id").toString());
				}
				if (null != map.get("cdpx")) {
					jsTree.setOrderno(Integer.parseInt(map.get("cdpx")
							.toString()));
				}
				if (null != map.get("cdmc")) {
					jsTree.setMenuName(map.get("cdmc").toString());
				}
				jsTrees.add(jsTree);
			}
		}
		return jsTrees;
	}
	
	@RequestMapping("/judgeMenuDelete")
	@ResponseBody
	public Integer judge(String menuId) throws Exception {
		List<Map<String, Object>> list=menuTreeService.findMenuNameByPid(menuId);
		int i=list.size();
		logger.debug("size=>"+i);
		return i;
	}

	/**
	 * @param menuId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/menuDelete")
	public ModelAndView removeMenu(String menuId) throws Exception {
		List<Map<String, Object>> list=menuTreeService.findMenuNameByPid(menuId);
		logger.debug("size=>"+list.size());
		if(list == null || list.size() == 0){
			menuTreeService.removePrivilege(menuId);
			menuTreeService.removePermission(menuId);
			menuTreeService.removeMenu(menuId);
		}
		return new ModelAndView("/view/sys-manage/menu");
	}
	
}
